$(function(){
    approved=false;
    var star=$(".info-nav input:checked").parent().attr("class")
    $(".info-right>div").each(function(){
         div=$(this).attr("class");
        if(div.indexOf(star)!=-1){
            $(this).css({"display":"block"});
        }else{
            $(this).css({"display":"none"});
        }
    });


    $(".info-nav dd").click(function(){
        var nav=$(this).attr("class");
        $(".info-right>div").each(function(){
            var div=$(this).attr("class");
            if(div.indexOf(nav)!=-1){
                $(this).css({"display":"block"});
            }else{
                $(this).css({"display":"none"});
            }
        })
    });



    $(".recode .head-next .che-box label").click(function () {
        var nav=$(this).attr("class");
        $(".info-right .recode .head-next").nextAll().each(function () {
            var div=$(this).attr("class");
            if(div.indexOf(nav)!=-1){
                $(this).css({"display":"block"});
            }else {
                $(this).css({"display":"none"});
            }
        });
    });


    // $(".recode .head-next .che-box:nth-child(1) label").click(function(){
    //     $(".experience-record").css({"display":"none"});
    //     $(".registration-record").css({"display":"block"});
    // });
    // $(".recode .head-next .che-box:nth-child(2) label").click(function(){
    //     $(".registration-record").css({"display":"none"});
    //     $(".experience-record").css({"display":"block"});
    // });


    $("#baocun").click(function () {
        console.log($('#editUser').serialize());
        $.ajax({
            type:"post",
            async:"true",
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            dataType: "json",//预期服务器返回的数据类型
            url: "/user/editUser" ,
            data: $('#editUser').serialize(),
            success: function (result) {
                console.log(result);//打印服务端返回的数据(调试用)
                // alert(result.msg);
                ShowMsg(result.msg);
            }
        });
    });


    function verificationPicFile(file) {
        var fileTypes = ["jpg", "png"];
        var filePath = file.value;
        var fileSize = 0;
        var fileMaxSize = 1024;//1M
        //当括号里面的值为0、空字符、false 、null 、undefined的时候就相当于false
        if(filePath){
            var isNext = false;
            var fileEnd = (/[.]/.exec(filePath)) ? /[^.]+$/.exec(filePath.toLowerCase()) : '';
            for (var i = 0; i < fileTypes.length; i++) {
                if (fileTypes[i] == fileEnd) {
                    isNext = true;
                    break;
                }
            }
            if (!isNext){
                ShowMsg('不接受此文件类型');


                file.value = "";
                approved=false;
                return false;
            }

            fileSize =file.files[0].size;
            var size = fileSize / 2048;
            if (size > fileMaxSize) {
                ShowMsg("文件大小不能大于2M！");

                file.value = "";
                approved=false;
                return false;
            }else if (size <= 0) {
                ShowMsg("文件小不能为0M！");

                file.value = "";
                approved=false;
                return false;
            }

            var filePic = file.files[0];
            var reader = new FileReader();
            reader.onload = function (e) {
                var data = e.target.result;
                //加载图片获取图片真实宽度和高度
                var image = new Image();
                image.οnlοad=function(){
                    var width = image.width;
                    var height = image.height;
                    if (width == 180 | height == 180){

                        ShowMsg("文件尺寸符合！");
                    }else {
                        ShowMsg("文件尺寸应为：180*180！");

                        file.value = "";
                        approved=false;
                        return false;
                    }
                };
                image.src= data;
            };
            reader.readAsDataURL(filePic);
        }
         objUrl = getObjectURL(file.files[0]); //获取图片的路径，该路径不是图片在本地的路径
        $("#pic").attr("src", objUrl); //将图片路径存入src中，显示出图片
        approved=true;
    };


    function upImg() {
        var pic = $('#headportrait')[0].files[0];
        var file = new FormData();
        file.append("headportrait",pic);
        $.ajax({
            url: "/user/uploadHeadportrait",
            type: "post",
            data: file,
            cache: false,
            contentType: false,
            processData: false,
            success: function(data) {

                ShowMsg(data.msg);
                if(data.code==200){
                    $(".touxiang").attr("src",objUrl)
                }
            }
        });
    }

    
    function getObjectURL(file) {
        var url = null;
        if (window.createObjectURL != undefined) { // basic
            url = window.createObjectURL(file);
        } else if (window.URL != undefined) { // mozilla(firefox)
            url = window.URL.createObjectURL(file);
        } else if (window.webkitURL != undefined) { // webkit or chrome
            url = window.webkitURL.createObjectURL(file);
        }
        return url;
    }

    $(".head-portrait .form-btn").click(function () {
        if (approved){
            upImg();
            approved=false;
        }
    })
    $("#headportrait").change(function(){
        verificationPicFile(this);
    })

    $("#pic").click(function(){
        console.log("df");
        $(this).next().click();
    });
});
